<template>
  <div style="padding:0px;">
    <!-- <el-alert :closable="false">
      <router-view />
    </el-alert>-->
    <div style="margin-top: 15px;">
      <el-input
        placeholder="请输入分公司id"
        v-model="soltid"
        style="width:25%;margin-bottom:15px"
        class="input-with-select"
      >
        <el-button @click="cxid" slot="append" icon="el-icon-search"></el-button>
      </el-input>
    </div>
    <el-table :data="tableData" border style="width: 100%;align" v-loading="loading">
      <el-table-column align="center" label="ID" width="180" prop="id">
        <!-- <template slot-scope="scope">
  
        <span style="margin-left: 10px">{{ scope.row.id }}</span>
        </template>-->
      </el-table-column>
      <el-table-column align="center" label="分公司名称" width="180" prop="name"></el-table-column>
      <el-table-column align="center" label="分公司管理员" width="180" prop="person"></el-table-column>
      <el-table-column align="center" label="电话号码" width="180" prop="telephone"></el-table-column>
      <el-table-column align="center" label="添加时间" width="100" prop="createTime">
        <!-- <template slot-scope="scope">
        <el-popover trigger="hover" placement="top">
  
          <div slot="reference" class="name-wrapper">
            <el-tag size="medium">{{ scope.row.usertype }}</el-tag>
          </div>
        </el-popover>
        </template>-->
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <v-pagination
      style="margin-top:20px"
      @page-change="pageChange"
      @page-size-change="pageSizeChange"
      :total="total"
      :page-size="pageSize"
      :layout="['total', 'prev', 'pager', 'next', 'sizer', 'jumper']"
    ></v-pagination>

    <!-- 我是编辑的表单页面 -->
    <el-dialog title="编辑分公司" :visible.sync="dialogFormVisible">
      <el-form :model="ruleForm" ref="ruleForm" :rules="rules">
        <el-form-item label="分公司名称" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分公司管理员" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.person" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话号码" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.telephone" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('ruleForm')">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<!-- 引入组件库 -->

<script>
import { hqfgs } from "@/api/zuzhijiagou";
import { getToken, gettype, getid } from "@/utils/auth";
import request from "@/utils/request";
import axios from "axios";
export default {
  name: "DragTable",

  data() {
    return {
      loading: true,
      formLabelWidth: "120px",
      dialogFormVisible: false,
      list: null,
      soltid: "",
      pm: {
        cid: "",
        page: 1,
        pageSize: 10
      },
      rules: {
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],

        person: [
          {
            required: true,
            message: "请输入分公司负责人的姓名",
            trigger: "blur"
          }
        ],
        telephone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          { min: 11, max: 11, message: "请输入11手机号", trigger: "blur" }
        ]
      },
      loading: false,
      pageIndex: 1,
      pageSize: 10,
      total: 1,

      tableData: [],
      ruleForm: {
        name: "",
        password: "",
        rppassword: "",
        person: "",
        telephone: "",
        id: getid()
      }
    };
  },
  created() {
    var id = getid();
    this.pm.cid = id;

    this.findList();
  },
  methods: {
    findList() {
      var resdata = {
        cid: this.pm.cid,
        page: this.pageIndex,
        pageSize: this.pageSize,
        tokens: getToken()
      };
      // 模拟后台接口
      this.loading = true;
      hqfgs(resdata)
        .then(response => {
          console.log(response);
          this.tableData = response.data.object;
          console.log(111111);

          this.total = response.data.total;
          this.loading = false;
        })
        .catch(e => {
          this.loading = false;
        });
    },
    // 编辑的操作
    handleEdit(index, row) {
      console.log(index, row);
      this.dialogFormVisible = true;
      this.ruleForm = row;
    },
    // 删除的操作
    handleDelete(index, row) {
      console.log(index, row);
      this.$confirm("此操作将永久删除该分公司, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          // axios.get("/account/del?id=" + row.id, {
          //   headers: {
          //     "Content-Type": "application/json",
          //     token: getToken(),
          //     type: 1
          //   }
          // });

          request(`branch/del/${row.id}`, {
            method: "get",
            headers: {
              "Content-Type": "application/json",
              token: getToken(),
              type: 1
            },
            params: ""
          })
            .then(res => {
              console.log(res);
              this.findList();
              this.$message({
                type: "success",
                message: "删除成功!"
              });
            })
            .catch(e => {
              console.log(e);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 查询单独的分公司
    cxid() {
      var that = this;

      if(this.soltid==""){
this.findList()
      }else{
         request(`branch/find/${this.soltid}`, {
        method: "get",
        headers: {
          "Content-Type": "application/json",
          token: getToken()
        },
        params: ""
      }).then(res => {
        console.log(232323);
        console.log(res.data.object);
        this.tableData = res.data.object;
        this.$message({
          type: "success",
          message: "加载成功!"
        });
      });
      }
     
    },
    pageChange(pageIndex) {
      this.pageIndex = pageIndex;
      this.findList();
      console.log(pageIndex);
    },
    pageSizeChange(pageSize) {
      this.pageIndex = 1;
      this.pageSize = pageSize;
      this.findList();
    },
    // 编辑删除的操作

    // 详细的编辑过程
    submitForm(formName) {
      var that = this;
      this.$refs[formName].validate(valid => {
        if (valid) {
          // b编辑
          request("branch/update", {
            method: "post",
            headers: { "Content-Type": "application/json" },
            data: {
              cid: that.ruleForm.cid,
              createTime: that.ruleForm.createTime,
              del: that.ruleForm.del,
              name: that.ruleForm.name,
              person: that.ruleForm.person,
              telephone: that.ruleForm.telephone,
              id: that.ruleForm.id
            }
          }).then(function(response) {
            this.dialogFormVisible = false;
            console.log(response);
            that.findList();
            that.$message({
              type: "success",
              message: "编辑成功!"
            });
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dialogFormVisible = false;
    }
  }
};
</script>

<style>
.sortable-ghost {
  opacity: 0.8;
  color: #fff !important;
  background: #42b983 !important;
}
.el-alert__content {
  width: 100%;
}
</style>

<style scoped>
.icon-star {
  margin-right: 2px;
}
.drag-handler {
  width: 20px;
  height: 20px;
  cursor: pointer;
}
.show-d {
  margin-top: 15px;
}
/* .el-alert__content {
  width: 50%;
} */
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}

.box-card {
  width: 480px;
}
.el-alert__content {
  width: 100%;
}
</style>
